package com.example.skillcinema

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.example.skillcinema.databinding.ActivityMainBinding
import com.example.skillcinema.ui.screens.main.viewmodels.MainViewModel
import com.oliverspryn.android.edgetoedge.extensions.applyTopAndSideInsets
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val viewModel: MainViewModel by viewModels<MainViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.mainFragmentContainerView) as? NavHostFragment
        val navController = navHostFragment?.navController
            ?: throw IllegalStateException("NavController not found!")
        binding.bottomNavigationView.setupWithNavController(navController)
        binding.mainContentFrameLayout.applyTopAndSideInsets()
        lifecycleScope.launch {
            viewModel.isDataLoadFlow.collect {
                if (it) {
                    with(binding) {
                        bottomNavigationView.visibility = View.VISIBLE
//                        navController.navigate(R.id.action_loadingFragment_to_homeFragment)
                    }
                }
            }
        }
    }
}